<?php

namespace App\Http\Controllers;

//use Illuminate\Http\Request;
use Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;

use App\Models\Book;
use App\Models\Bags;
use App\Models\BagsLiuyan;
use Session;
use Redirect;

class BagsController extends Controller
{

    public function newoffer($bookid)
    {
        //闲置图书放入书包 提交页面
        $books = Book::where('book_id', $bookid)->first();
        $uid = Session::get('nameid'); //登录
        $bags = Bags::where('book_id', $bookid)->where('uid',$uid)->first();
        if(empty($bags)){
            //为空，说明会员没有出售这本书，去添加页面
            return view('books.layouts.new_offer', array('books' => $books));
        }else{
            //提示已经在出售
            return '本图书已经在您的书城中出售了，请不要重复添加。';
        }
    }

    public function saveaddbags(Request $request)
    {
        //保存到书包
        $bags = new Bags;

        //获得用户的uid
        //$name = Session::get('name');
        $uid = Session::get('nameid'); 

        $bags->book_id   = request::input('book_id');
        $bags->uid       = $uid;
        $bags->book_name   = request::input('book_name');
        $bags->zuozhe   = request::input('zuozhe');
        $bags->isbn   = request::input('isbn');
        $bags->otype     = request::input('otype'); //J借阅，E交换，S出售，Z赠送，B以上均可
        $bags->scope     = request::input('scope'); //L当地(同城),R当地或异地
        $bags->condition = request::input('condition'); //新旧程度
        //$bags->price     = request::input('price'); //原价
        $bags->nowprice     = request::input('nowprice'); //售价
        $bags->num     = request::input('num'); //出售图书数量
        $bags->beizhu     = request::input('beizhu'); //备注
        $bags->add_time = time(); //获取当前时间戳

        $bags->save();
        
        //跳转到图书信息页面
        //$bookid = request::input('book_id');
        //return Redirect::to('info/'. $bookid);

        //跳转到书包列表页面
        return Redirect::to('u/'. $uid . '/bags');
    }

    public function editbags($id)
    {
        //修改书包里的图书 编辑
        $uid = Session::get('nameid');
        $bags =Bags::where('uid', $uid)->where('id',$id)->first();
        $books = Book::where('book_id', $bags->book_id)->first();
        return view('books.layouts.editbags', array('books' => $books, 'bags' => $bags));
    }

    public function saveeditbags(Request $request)
    {
        //修改书包里的图书 修改保存

        //获得用户的uid
        //$name = Session::get('name');
        $uid = Session::get('nameid'); 

        //获得书包里的id编号
        $id = request::input('bagsid');

        $bags =Bags::where('uid', $uid)->where('id',$id)->first();

        if(count($bags)==0){
            //如果修改的信息不属于本登录会员(数据不存在)，跳转到书包列表页面
            return Redirect::to('u/'. $uid . '/bags');
        }
        else{
            
            $bags->book_id   = request::input('book_id');
            $bags->book_name   = request::input('book_name');
            $bags->zuozhe   = request::input('zuozhe');
            $bags->isbn   = request::input('isbn');
            $bags->otype     = request::input('otype'); //J借阅，E交换，S出售，Z赠送，B以上均可
            $bags->scope     = request::input('scope'); //L当地(同城),R当地或异地
            $bags->condition = request::input('condition'); //N全新,R较新,H半新,O较旧
            $bags->nowprice     = request::input('nowprice'); //估价
            $bags->num     = request::input('num'); //出售图书数量
            $bags->beizhu     = request::input('beizhu'); //备注
            $bags->add_time = time(); //获取当前时间戳

            $bags->save();
        
            return Redirect::to('u/'. $uid . '/bags'); 
        }
        
    }

    public function removebags($id)
    {
        //移除书包里的图书
        $uid = Session::get('nameid');
        //删除会员出售的书籍
        $bags = Bags::where('id', $id)->where('uid', $uid)->delete();
        //删除出售的书籍对应的留言
        $bagsliuyans = BagsLiuyan::where('bagsid', $id)->where('uid', $uid)->delete(); 

        return Redirect::to('u/'. $uid . '/bags'); 
    }

}
